//引入依赖的配置文件模块
require(["config"], function() {
	//引入依赖模块
	require(["jquery", "template", "swiper", "header", "carousel", "cookie"], function($, template, Swiper) {

		function Index() {
			this.loadCarousel();
			this.maxtop_carousel();
			this.loadImg();
			this.loadUsername();
			this.username = null;
		}

		$.extend(Index.prototype, {
			loadCarousel: function() {
				$(".carousel").carousel({
					imgs: [{
						href: "#",
						src: "/images/banner.jpg"
					}, {
						href: "#",
						src: "/images/banner1.jpg"
					}],
					width: 1253,
					height: 588,
					duration: 3000
				});
			},
			//从cookie中读取用户名并放在首页
			loadUsername() {
				$.cookie.json = true;
				this.username = $.cookie("login-user");
				if(this.username) {
					//					var data = {
					//						list: this.username
					//					};

					var html = template("already_login", {
						username: this.username
					});
					$(".login_title").html(html);
					//					console.log(data)

				}
			},

			maxtop_carousel() {
				$.ajax("http://rap2api.taobao.org/app/mock/94517/api/max_carousel_right")
					.done(data => {
						var data = {
							list: data.res_body.list
						}
						//												data.list.splice(0, data.list.length - 1)
						//						console.log(data)
						var html = template("maxtop_carousel", data);
						$(".swiper-container").prepend(html);

						//加载轮播图右边的列表
						this.maxtop_right_carousel();
						this.loadSwiper();
					});
			},
			maxtop_right_carousel() {
				$.ajax("http://rap2api.taobao.org/app/mock/94517/api/max_carousel_right")
					.done(data => {
						var data = {
							list: data.res_body.list
						};
						var html = template("maxtop_right_carousel", data);
						$(".maxtop_right_carousel").html(html);
					});
			},

			loadSwiper() {
				/*	var mys = new Swiper('.swiper-container', {
					direction: 'vertical', // 垂直切换选项
					loop: true, // 循环模式选项
					autoplay: true,

					// 如果需要分页器
					pagination: {
						el: '.swiper-pagination',
					},
					//
					//					// 如果需要前进后退按钮
					navigation: {
						nextEl: '.swiper-button-next',
						prevEl: '.swiper-button-prev'
					},

					// 如果需要滚动条
					//					scrollbar: {
					//						el: '.swiper-scrollbar',
					//					},
				});
*/
				new Swiper(".swiper-container", {
					autoplay: true,
					//					direction: "vertical",
					navigation: {
						nextEl: '.swiper-button-next',
						prevEl: '.swiper-button-prev',
					},
					pagination: {
						el: ".swiper-pagination"
					}
				});
			},
			loadImg() {
				$.ajax("http://rap2api.taobao.org/app/mock/94517/api/max_pic")
					.done(data => {
						var data = {
							list: data.res_body.list
						}
						//						console.log(data.list)
						var html = template("_maxBpic", data);
						$(".maxBpic_bottom").html(html);
					});
			}

		});
		new Index();
	});
});